﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Infragistics.Win;
using Infragistics.Win.UltraWinGrid;
using Infragistics.Excel;
namespace BYManageInfoSystem
{
    public partial class Frm_FacMakeInvoiceDetailSplit : Form
    {
        private DGBY.BLL.BYCompanyTab m_bllBYCompanyTab = new DGBY.BLL.BYCompanyTab();
        private DGBY.BLL.FactoryDataTab m_bllFactoryDataTab = new DGBY.BLL.FactoryDataTab();
        private DGBY.BLL.FacMakeInvoiceSplitTab m_bllFacMakeInvoiceSplitTab = new DGBY.BLL.FacMakeInvoiceSplitTab();

        public class SplitGoodsInfo
        {
            public Decimal dKaiPiaoTotalPrice = 0;
            public Decimal dWithTaxPrice = 0;
            
            public Decimal dShouldReturnTaxValue = 0;
            public Decimal dBGJinE = 0;
            public Decimal dSellPrice = 0;
            public Decimal dCost = 0;
            public Decimal dProfit = 0;
            public int nNum = 0;
            public string strContractNo = "";
            //public string strCarBoxNo = "";
            //public string strBGDNo = "";
            //public DateTime dtSB = new DateTime();

            public long lSeq = 0;


            public void Reset()
            {
                dKaiPiaoTotalPrice = 0;
                dWithTaxPrice = 0;

                dShouldReturnTaxValue = 0;
                dBGJinE = 0;
                dSellPrice = 0;
                dCost = 0;
                dProfit = 0;
                nNum = 0;
                strContractNo = "";
                //strCarBoxNo = "";
                //strBGDNo = "";
                lSeq = 0;
            }
        }


        public class CustomMergedCellEvaluator : Infragistics.Win.UltraWinGrid.IMergedCellEvaluator
        {
            UltraGridColumn m_BaseColumn = null;
            public CustomMergedCellEvaluator(UltraGrid UltraGridTemp)
            {
                foreach (UltraGridColumn column2 in UltraGridTemp.DisplayLayout.Bands[0].Columns)
                {
                    string str = column2.Header.Caption;
                    if (str == "合同编号")
                    {
                        m_BaseColumn = column2;
                        break;
                    }
                }
            }
            // 合并日期相同的单元格（无需时间精确相同）
            public bool ShouldCellsBeMerged(UltraGridRow row1, UltraGridRow row2, UltraGridColumn column)
            {
                string str1 = (string)row1.GetCellValue(m_BaseColumn);
                string str2 = (string)row2.GetCellValue(m_BaseColumn);

                return str1 == str2;

            }

        }

        public class CustomMergedCellEvaluator2 : Infragistics.Win.UltraWinGrid.IMergedCellEvaluator
        {
            

            // 合并日期相同的单元格（无需时间精确相同）
            public bool ShouldCellsBeMerged(UltraGridRow row1, UltraGridRow row2, UltraGridColumn column)
            {
                long lSeq1 = (long)row1.GetCellValue(column);
                long lSeq2 = (long)row2.GetCellValue(column);

                return lSeq1 == lSeq2;

            }

        }
        public Frm_FacMakeInvoiceDetailSplit()
        {
            InitializeComponent();
        }
        public void InitUltraWinGridSetting()
        {
            //this.ultraGrid1.DisplayLayout.Override.RowSelectors = Infragistics.Win.DefaultableBoolean.True;
            //this.ultraGrid1.DisplayLayout.Override.RowSelectorNumberStyle = Infragistics.Win.UltraWinGrid.RowSelectorNumberStyle.VisibleIndex;
            //this.ultraGrid1.DisplayLayout.Override.RowSelectorWidth = 40;

        }

        public void LocalPrintPreveiw()
        {
            Infragistics.Shared.ResourceCustomizer rc = Infragistics.Win.Printing.Resources.Customizer;//Resources.Customizer;
            rc.SetCustomizedString("PrintPreview_DialogCaption", "打印预览");
            rc.SetCustomizedString("PrintPreview_Tool_Print", "打印(&P)");
            rc.SetCustomizedString("PrintPreview_Tool_ClosePreview", "关闭(&C)");
            rc.SetCustomizedString("PrintPreview_Tool_ContextMenuPreviewZoom", "显示比例");
            rc.SetCustomizedString("PrintPreview_Tool_Current_Page", "当前页");
            rc.SetCustomizedString("PrintPreview_Tool_Exit", "退出(&X)");
            rc.SetCustomizedString("PrintPreview_Tool_First_Page", "第一页");
            rc.SetCustomizedString("PrintPreview_Tool_Go_To", "跳至");
            rc.SetCustomizedString("PrintPreview_Tool_Last_Page", "最后一页");
            rc.SetCustomizedString("PrintPreview_Tool_Next_Page", "下一页");
            rc.SetCustomizedString("PrintPreview_Tool_Previous_Page", "前一页");
            rc.SetCustomizedString("PrintPreview_Tool_Next_View", "下一视图(&N)");
            rc.SetCustomizedString("PrintPreview_Tool_Previous_View", "前一视图(&P)");
            rc.SetCustomizedString("PrintPreview_Tool_Hand_Tool", "手型工具(&H)");
            rc.SetCustomizedString("PrintPreview_Tool_Page_Setup", "页面设置(&U)");
            rc.SetCustomizedString("PrintPreview_Tool_Snapshot_Tool", "快照工具(&S)");
            rc.SetCustomizedString("PrintPreview_Tool_View", "视图(&V)");
            rc.SetCustomizedString("PrintPreview_Tool_Whole_Page", "合适页");
            rc.SetCustomizedString("PrintPreview_Tool_Zoom", "缩放(&Z)");
            rc.SetCustomizedString("PrintPreview_Tool_Zoom_In", "放大");

            rc.SetCustomizedString("PrintPreview_Tool_Zoom_Out", "缩小");

            rc.SetCustomizedString("PrintPreview_ToolCategory_Context_Menus", "上下文菜单");
            rc.SetCustomizedString("PrintPreview_ToolCategory_File", "文件");
            rc.SetCustomizedString("PrintPreview_ToolCategory_Menus", "菜单");
            rc.SetCustomizedString("PrintPreview_ToolCategory_Tools", "工具栏");
            rc.SetCustomizedString("PrintPreview_ToolCategory_View", "视图");
            rc.SetCustomizedString("PrintPreview_ToolCategory_Zoom_Mode", "缩放模式");

            rc.SetCustomizedString("PrintPreview_ToolTip_ClosePreview", "关闭");
            rc.SetCustomizedString("PrintPreview_ToolTip_Zoom", "缩放");
            rc.SetCustomizedString("StatusBar_Page_X_OF_X", "页:{0}/{1}");

            rc.SetCustomizedString("CustomizeImg_ToolBar_MenuBar", "菜单");
            rc.SetCustomizedString("CustomizeImg_ToolBar_Standard", "标准");
            rc.SetCustomizedString("CustomizeImg_ToolBar_View", "视图");
            rc.SetCustomizedString("PrintPreview_Tool_File", "文件(&F)");
            rc.SetCustomizedString("PrintPreview_Tool_Tools", "工具(&T)");
            rc.SetCustomizedString("PrintPreview_Tool_Dynamic_Zoom_Tool", "动态缩放工具(&D)");
            rc.SetCustomizedString("PrintPreview_Tool_Zoom_Out_Tool", "缩小工具");
            rc.SetCustomizedString("PrintPreview_Tool_Zoom_In_Tool", "放大工具");
            // rc.SetCustomizedString("PrintPreview_Tool_Page_Layout","菜单");
            rc.SetCustomizedString("PreviewRowColSelection_Cancel", "取消");
            rc.SetCustomizedString("PreviewRowColSelection_SelectedPages", "{0} x {1} 页");

            rc.SetCustomizedString("PreviewRowColSelection_Cancel", "取消");
            rc.SetCustomizedString("PrintPreview_Tool_Page_Width", "页宽");
            rc.SetCustomizedString("PrintPreview_ZoomListItem_MarginWidth", "文字宽度");
            rc.SetCustomizedString("PrintPreview_ZoomListItem_PageWidth", "页宽");
            rc.SetCustomizedString("PrintPreview_ZoomListItem_WholePage", "合适页");
            rc.SetCustomizedString("PrintPreview_Tool_Page_Layout", "页面布局");
            rc.SetCustomizedString("PrintPreview_Tool_Margin_Width", "文字宽度");
            rc.SetCustomizedString("ContextMenuPreviewHand", "缩放视图");


            rc.SetCustomizedString("PrintPreview_Tool_Reduce_Page_Thumbnails", "缩小");
            rc.SetCustomizedString("PrintPreview_Tool_Show_Page_Numbers", "显示页号");
            rc.SetCustomizedString("PrintPreview_Tool_ContextMenuThumbnail", "缩略图");
            rc.SetCustomizedString("PrintPreview_Tool_Enlarge_Page_Thumbnails", "放大");
            rc.SetCustomizedString("PrintPreview_Tool_Thumbnails", "缩略图");
            rc.SetCustomizedString("PrintPreview_Tool_Continuous", "连续排序");
            //说明
            rc.SetCustomizedString("StatusBar_DynamicZoom_Instructions", "单击并拖动进行缩放操作");
            rc.SetCustomizedString("StatusBar_Page_X_OF_X", "当前页: {0} / {1}");
            rc.SetCustomizedString("StatusBar_SnapShot_Instructions", "单击并拖动,系统将选定矩型区域复制到剪帖板");
            rc.SetCustomizedString("StatusBar_ZoomIn_Instructions", "单击并拖动,系统将放大选定矩型区域");
            rc.SetCustomizedString("StatusBar_ZoomOut_Instructions", "单击并拖动,系统将缩小选定矩型区域");
            rc.SetCustomizedString("StatusBar_Hand_Instructions", "单击并拖动以便显示更多内容");
        }
        private void InitCombox()
        {
            DataSet ds = m_bllBYCompanyTab.GetList("");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                this.comboBox2Comp.DataSource = ds.Tables[0];
                this.comboBox2Comp.DisplayMember = "经营单位名称";
                this.comboBox2Comp.ValueMember = "BYCompanyTabID";
                this.comboBox2Comp.SelectedIndex = 0;
            }

            ds = m_bllFactoryDataTab.GetList("");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                this.comboBoxFac.DataSource = ds.Tables[0];

                this.comboBoxFac.ValueMember = "FactoryDataTabID";
                this.comboBoxFac.DisplayMember = "工厂名称";
                this.comboBoxFac.SelectedIndex = 0;
            }
        }
        private void Frm_FacMakeInvoiceDetailSplit_Load(object sender, EventArgs e)
        {
            InitCombox();
            LocalPrintPreveiw();
            InitUltraWinGridSetting();
        }

        void ConvertStringToMultiLineFirst()
        {
            foreach (UltraGridColumn column2 in ultraGrid1.DisplayLayout.Bands[0].Columns)
            {
                string str = column2.Header.Caption;
                if (str == "集装箱柜号")
                {
                    int nRowsCount = this.ultraGrid1.Rows.Count;
                    for (int i = 0; i < nRowsCount; i++)
                    {
                        string strSource = this.ultraGrid1.Rows[i].GetCellValue(column2).ToString();
                        ConvertStringToMultiLineSecond(strSource, i);
                    }
                }
            }
        }

        private void ConvertStringToMultiLineSecond(string strSource, int nRowIndex)
        {
            string str = strSource.ToString();
            string[] arr = str.Split(',');
            string tempa = "";
            int n = 0;
            foreach (string ch in arr)
            {

                if (ch != "")
                {
                    if (n == 0)
                        tempa += ch;
                    else
                        tempa += "\n" + ch;
                }

                n++;
            }

            this.ultraGrid1.Rows[nRowIndex].Cells["集装箱柜号"].Value = tempa;
        }

        private void ShowAllRecord(string strWhere)
        {
            DataSet ds = m_bllFacMakeInvoiceSplitTab.GetFacMakeInvoiceSplitList(strWhere);
            //List<long> nContractLst = new List<long>();
            long lChange = 0;
            long lLastValue = 0;
            
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                //重新转换序号，变成从1开始
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    string strSeq = ds.Tables[0].Rows[i]["序号"].ToString();
                    long lSeq = Convert.ToInt64(strSeq, 10);
                    if (lLastValue != lSeq)
                    {
                        lChange++;
                    }

                    ds.Tables[0].Rows[i]["序号"] = lChange;
                    lLastValue = lSeq;
                }

                DataTable dtNew = null;
                dtNew = ds.Tables[0].Clone();
                int nAcculateNum = 0;
                SplitGoodsInfo sgiTemp = new SplitGoodsInfo();
                SplitGoodsInfo sgiTotal = new SplitGoodsInfo();
                //Decimal dTotal = 0;
                //Decimal dTotalAll = 0;
                int nRowsCount = ds.Tables[0].Rows.Count;
                for (int i = 0; i < nRowsCount; i++)
                {
                    dtNew.Rows.Add(ds.Tables[0].Rows[i].ItemArray);

                    string strName = ds.Tables[0].Rows[i]["报关金额"].ToString();
                    sgiTemp.dBGJinE = Convert.ToDecimal(strName) + sgiTemp.dBGJinE;
                    sgiTotal.dBGJinE = Convert.ToDecimal(strName) + sgiTotal.dBGJinE;

                    strName = ds.Tables[0].Rows[i]["开票总价"].ToString();
                    sgiTemp.dKaiPiaoTotalPrice = Convert.ToDecimal(strName) + sgiTemp.dKaiPiaoTotalPrice;
                    sgiTotal.dKaiPiaoTotalPrice = Convert.ToDecimal(strName) + sgiTotal.dKaiPiaoTotalPrice;

                    strName = ds.Tables[0].Rows[i]["数量"].ToString();
                    sgiTemp.nNum = Convert.ToInt32(strName, 10) + sgiTemp.nNum;
                    sgiTotal.nNum = Convert.ToInt32(strName, 10) + sgiTotal.nNum;

                    strName = ds.Tables[0].Rows[i]["售价"].ToString();
                    sgiTemp.dSellPrice = Convert.ToDecimal(strName) + sgiTemp.dSellPrice;
                    sgiTotal.dSellPrice = Convert.ToDecimal(strName) + sgiTotal.dSellPrice;

                    strName = ds.Tables[0].Rows[i]["成本"].ToString();
                    sgiTemp.dCost = Convert.ToDecimal(strName) + sgiTemp.dCost;
                    sgiTotal.dCost = Convert.ToDecimal(strName) + sgiTotal.dCost;

                    strName = ds.Tables[0].Rows[i]["利润"].ToString();
                    sgiTemp.dProfit = Convert.ToDecimal(strName) + sgiTemp.dProfit;
                    sgiTotal.dProfit = Convert.ToDecimal(strName) + sgiTotal.dProfit;

                    strName = ds.Tables[0].Rows[i]["含税价"].ToString();
                    sgiTemp.dWithTaxPrice = Convert.ToDecimal(strName) + sgiTemp.dWithTaxPrice;
                    sgiTotal.dWithTaxPrice = Convert.ToDecimal(strName) + sgiTotal.dWithTaxPrice;

                    strName = ds.Tables[0].Rows[i]["应退税金额"].ToString();
                    sgiTemp.dShouldReturnTaxValue = Convert.ToDecimal(strName) + sgiTemp.dShouldReturnTaxValue;
                    sgiTotal.dShouldReturnTaxValue = Convert.ToDecimal(strName) + sgiTotal.dShouldReturnTaxValue;
                    
                    sgiTemp.strContractNo = ds.Tables[0].Rows[i]["合同编号"].ToString();

                    //sgiTemp.strBGDNo = ds.Tables[0].Rows[i]["报关单号"].ToString();
                    //sgiTemp.strCarBoxNo = ds.Tables[0].Rows[i]["集装箱柜号"].ToString();
                    //sgiTemp.dtSB = (DateTime)ds.Tables[0].Rows[i]["申报日期"];

                    sgiTemp.lSeq = Convert.ToInt64(ds.Tables[0].Rows[i]["序号"].ToString(), 10);
                    sgiTotal.lSeq = 0;

                    if (i < (nRowsCount - 1) && ds.Tables[0].Rows[i]["序号"].ToString() == ds.Tables[0].Rows[i + 1]["序号"].ToString())
                    {
                        nAcculateNum++;
                        
                    }
                    else  if(i == nRowsCount - 1)//最后一行
                    {
                        DataRow dr1 = null;
                        if (nAcculateNum > 0)
                        {
                            dr1 = dtNew.NewRow();
                            //object[] itemArray = new object[ds.Tables[0].Columns.Count];
                            //ds.Tables[0].Rows[i].ItemArray.CopyTo(itemArray, 0);


                            //dr1.ItemArray = itemArray;
                            dr1["序号"] = sgiTemp.lSeq;
                            dr1["报关金额"] = sgiTemp.dBGJinE;
                            dr1["开票总价"] = sgiTemp.dKaiPiaoTotalPrice;
                            dr1["数量"] = sgiTemp.nNum;
                            dr1["售价"] = sgiTemp.dSellPrice;
                            dr1["成本"] = sgiTemp.dCost;
                            dr1["利润"] = sgiTemp.dProfit;
                            dr1["含税价"] = sgiTemp.dWithTaxPrice;
                            dr1["应退税金额"] = sgiTemp.dShouldReturnTaxValue;
                            dr1["合同编号"] = sgiTemp.strContractNo;
                            //dr1["报关单号"] = sgiTemp.strBGDNo;
                            
                            //dr1["集装箱柜号"] = sgiTemp.strCarBoxNo;
                            //dr1["申报日期"] = sgiTemp.dtSB;
                            
                            dr1["商品名称"] = "小计";

                            dtNew.Rows.Add(dr1.ItemArray);
                        }


                        dr1 = dtNew.NewRow();
                        //itemArray = new object[ds.Tables[0].Columns.Count];
                        //ds.Tables[0].Rows[i].ItemArray.CopyTo(itemArray, 0);


                        //dr1.ItemArray = itemArray;
                        dr1["序号"] = 0;
                        dr1["报关金额"] = sgiTotal.dBGJinE;
                        dr1["开票总价"] = sgiTotal.dKaiPiaoTotalPrice;
                        dr1["数量"] = sgiTotal.nNum;
                        dr1["售价"] = sgiTotal.dSellPrice;
                        dr1["成本"] = sgiTotal.dCost;
                        dr1["利润"] = sgiTotal.dProfit;
                        dr1["含税价"] = sgiTotal.dWithTaxPrice;
                        dr1["应退税金额"] = sgiTotal.dShouldReturnTaxValue;
                        dr1["合同编号"] = "";
                        dr1["商品名称"] = "合计";

                        dtNew.Rows.Add(dr1.ItemArray);

                      
                        sgiTemp.Reset();
                        nAcculateNum = 0;
                    }
                    else
                    {
                        if(nAcculateNum > 0)
                        {
                            DataRow dr1 = dtNew.NewRow();
                            //object[] itemArray = new object[ds.Tables[0].Columns.Count];
                            //ds.Tables[0].Rows[i].ItemArray.CopyTo(itemArray, 0);


                            //dr1.ItemArray = itemArray;
                            dr1["序号"] = sgiTemp.lSeq;
                            dr1["报关金额"] = sgiTemp.dBGJinE;
                            dr1["开票总价"] = sgiTemp.dKaiPiaoTotalPrice;
                            dr1["数量"] = sgiTemp.nNum;
                            dr1["售价"] = sgiTemp.dSellPrice;
                            dr1["成本"] = sgiTemp.dCost;
                            dr1["利润"] = sgiTemp.dProfit;
                            dr1["含税价"] = sgiTemp.dWithTaxPrice;
                            dr1["应退税金额"] = sgiTemp.dShouldReturnTaxValue;

                            
                            dr1["合同编号"] = sgiTemp.strContractNo;
                            //dr1["集装箱柜号"] = sgiTemp.strCarBoxNo;
                            //dr1["申报日期"] = sgiTemp.dtSB;
                            //dr1["报关单号"] = sgiTemp.strBGDNo;

                            dr1["商品名称"] = "小计";
                            dtNew.Rows.Add(dr1.ItemArray);
                        }
                        sgiTemp.Reset();
                        nAcculateNum = 0;

                       
                    }

                    
                    
                }

                ultraGrid1.DataSource = dtNew;
                this.ultraGrid1.DisplayLayout.Bands[0].Columns["集装箱柜号"].CellMultiLine = Infragistics.Win.DefaultableBoolean.True;
                //ConvertStringToMultiLineFirst();
                ultraGrid1.DisplayLayout.Bands[0].Columns[0].Hidden = true;
                ultraGrid1.DisplayLayout.Bands[0].Columns[1].Hidden = true;
                
            }
        }

        private void buttonQuery_Click(object sender, EventArgs e)
        {
            ShowAllRecord(@"PaidanRegisterTab.BYCompanyTabID = '" + this.comboBox2Comp.SelectedValue + "'"
                                                                           + " and FacConPeoAndTelTab.FactoryDataTabID = '" + this.comboBoxFac.SelectedValue + "'"
                                                                           + " and BGDComeBackTab.SBDate >= '" + this.dateTimePickerFrom.Value + "'" + " and BGDComeBackTab.SBDate <= '" + this.dateTimePickerTo.Value + "'");


        }

        private void ultraGrid1_InitializeRow(object sender, InitializeRowEventArgs e)
        {
            
            ultraGrid1.DisplayLayout.Override.CellAppearance.TextHAlign = HAlign.Center;
            ultraGrid1.DisplayLayout.Override.CellAppearance.TextVAlign = VAlign.Middle;
            ultraGrid1.DisplayLayout.Override.MergedCellAppearance.TextHAlign = HAlign.Center;
            ultraGrid1.DisplayLayout.Override.MergedCellAppearance.TextVAlign = VAlign.Middle;

            ultraGrid1.DisplayLayout.Bands[0].Columns["合同编号"].MergedCellStyle = MergedCellStyle.Always;
            ultraGrid1.DisplayLayout.Bands[0].Columns["申报日期"].MergedCellStyle = MergedCellStyle.Always;
            ultraGrid1.DisplayLayout.Bands[0].Columns["报关单号"].MergedCellStyle = MergedCellStyle.Always;
            ultraGrid1.DisplayLayout.Bands[0].Columns["集装箱柜号"].MergedCellStyle = MergedCellStyle.Always;
            ultraGrid1.DisplayLayout.Bands[0].Columns["序号"].MergedCellStyle = MergedCellStyle.Always;
            ultraGrid1.DisplayLayout.Bands[0].Columns["最后开票日期"].MergedCellStyle = MergedCellStyle.Always;

            ultraGrid1.DisplayLayout.Bands[0].Columns["合同编号"].MergedCellEvaluator = new
   CustomMergedCellEvaluator(this.ultraGrid1);
            ultraGrid1.DisplayLayout.Bands[0].Columns["申报日期"].MergedCellEvaluator = new
   CustomMergedCellEvaluator(this.ultraGrid1);
            ultraGrid1.DisplayLayout.Bands[0].Columns["报关单号"].MergedCellEvaluator = new
   CustomMergedCellEvaluator(this.ultraGrid1);
            ultraGrid1.DisplayLayout.Bands[0].Columns["集装箱柜号"].MergedCellEvaluator = new
   CustomMergedCellEvaluator(this.ultraGrid1);
            ultraGrid1.DisplayLayout.Bands[0].Columns["最后开票日期"].MergedCellEvaluator = new
CustomMergedCellEvaluator(this.ultraGrid1);

            ultraGrid1.DisplayLayout.Bands[0].Columns["序号"].MergedCellEvaluator = new
CustomMergedCellEvaluator2();
        
        }

        private void buttonExportToExcel_Click(object sender, EventArgs e)
        {
            DataTable dt = (DataTable)ultraGrid1.DataSource;

            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    SaveFileDialog sfd = new SaveFileDialog();
                    sfd.Filter = "Excel文件(*.xls)|*.xls";
                    string outFile = "";

                    if (sfd.ShowDialog() == DialogResult.OK)
                    {
                        outFile = sfd.FileName;
                    }
                    else
                    {
                        return;
                    }

                    ultraGridExcelExporter1.Export(ultraGrid1, outFile);
                }
            }
        }

        private void buttonPrint_Click(object sender, EventArgs e)
        {
            foreach (UltraGridColumn ugc in ultraGrid1.DisplayLayout.Bands[0].Columns)
                ugc.Width++;
            this.ultraGridPrintDocument2.Grid = this.ultraGrid1;
            this.ultraPrintPreviewDialog2.Document = this.ultraGridPrintDocument2;
            this.ultraPrintPreviewDialog2.ShowDialog(this);
        }

        private void ultraGrid1_InitializeLayout(object sender, InitializeLayoutEventArgs e)
        {
            ultraGrid1.DisplayLayout.Bands[0].UseRowLayout = true;
            ultraGrid1.DisplayLayout.Override.RowSizing = RowSizing.AutoFree;
            foreach (UltraGridColumn ugc in ultraGrid1.DisplayLayout.Bands[0].Columns)
                ugc.PerformAutoResize(PerformAutoSizeType.AllRowsInBand);
        }

        private void ultraGridExcelExporter1_BeginExport(object sender, Infragistics.Win.UltraWinGrid.ExcelExport.BeginExportEventArgs e)
        {
            Worksheet ws = e.CurrentWorksheet;



            ws.Rows[1].Cells[5].Value = this.comboBoxFac.Text + "开票明细";
            ws.Rows[1].Cells[6].Value = DateTime.Now.Date;


            ws.Rows[2].Cells[0].Value = @"注：以下是开给“" + this.comboBox2Comp.Text + @"”";
            e.CurrentRowIndex = 3;
        }

        private void ultraGridExcelExporter1_CellExported(object sender, Infragistics.Win.UltraWinGrid.ExcelExport.CellExportedEventArgs e)
        {
            // Get the cell being exporter
            UltraGridCell cell = e.GridRow.Cells[e.GridColumn];

            // Get the Merged cells. 
            UltraGridCell[] mergedCells = cell.GetMergedCells();


            // Check to see if there are any merged cells. If not, we don't need to do anything. 
            if (mergedCells != null)
            {
                // There are merged cells. Check to see if the cell being exported is the first one. 
                bool isFirstCell = true;
                foreach (UltraGridCell mergedCell in mergedCells)
                {
                    if (cell.Row.Index > mergedCell.Row.Index)
                    {
                        // If the cell being exported has a higher index that any of the cells
                        // it is merged with, them it's not the first cell and we should already
                        // have handled the merge. 
                        isFirstCell = false;
                        break;
                    }
                }

                // If it is the first cell, merge this cell with the cells below it based on the 
                // count of the merged cells. 
                if (isFirstCell)
                {
                    int rowIndex = e.CurrentRowIndex;
                    int colIndex = e.CurrentColumnIndex;
                    Infragistics.Excel.WorksheetMergedCellsRegion mergedRegion = e.CurrentWorksheet.MergedCellsRegions.Add(
                        rowIndex,
                        colIndex,
                        rowIndex + (mergedCells.Length - 1),
                        colIndex);
                    mergedRegion.CellFormat.Alignment = Infragistics.Excel.HorizontalCellAlignment.Center;
                    mergedRegion.CellFormat.VerticalAlignment = Infragistics.Excel.VerticalCellAlignment.Center;
                }

            }
        }


    }
}